Vector¶

Vector is base class of other vector like classes. It represents a series of vectors.

InĀ [1]:
from py3d import Vector
Vector()
Out[1]:
Vector([], dtype=float64)
InĀ [3]:
Vector([0, 0, 0, 0])
Out[3]:
Vector([0, 0, 0, 0])
InĀ [5]:
Vector([1, 2, -4])
Out[5]:
Vector([ 1,  2, -4])
InĀ [7]:
Vector([1, 2, 3]).tile(2)
Out[7]:
Vector([[1, 2, 3],
        [1, 2, 3]])
InĀ [9]:
import py3d
py3d.Vector([1, 2, 0, 3])
Out[9]:
Vector([1, 2, 0, 3])
InĀ [10]:
Vector([1]).tile(2)
Out[10]:
Vector([[1],
        [1]])

Get unit vector

InĀ [12]:
import py3d
V32 = py3d.Vector([
    [1, 3],
    [2, -9],
    [0.1, -3]])
V32.U
Out[12]:
Vector([[ 0.31622777,  0.9486833 ],
        [ 0.21693046, -0.97618706],
        [ 0.03331483, -0.99944491]])

Get length of vectors

InĀ [13]:
V32.L
Out[13]:
Vector([3.16227766, 9.21954446, 3.0016662 ])
InĀ [15]:
from numpy import allclose
assert allclose(V32.U.L, [[1], [1], [1]])

Get homogeneous vector

InĀ [16]:
V32.H
Out[16]:
Vector3([[ 1. ,  3. ,  1. ],
         [ 2. , -9. ,  1. ],
         [ 0.1, -3. ,  1. ]])

Get a series of random vectors

InĀ [18]:
import py3d
py3d.rand(2, 3, 4)
Out[18]:
Vector4([[[0.4775848 , 0.23788283, 0.96181477, 0.56249829],
          [0.38692563, 0.84996546, 0.78497679, 0.25219111],
          [0.12706333, 0.58328646, 0.60320586, 0.39345616]],

         [[0.85899961, 0.67085748, 0.41731089, 0.91689376],
          [0.11291986, 0.77471412, 0.38475038, 0.04572628],
          [0.81475352, 0.87258385, 0.22341251, 0.19649436]]])
InĀ [20]:
import py3d
py3d.Vector([[0, 0], [1, 2], [3, 4]]).diff()
Out[20]:
Vector([[1, 2],
        [2, 2]])

Get a flattened copy of a vector

InĀ [22]:
import py3d
py3d.Vector([[[1,2,3]],[[4,5,6]]]).flatten()
Out[22]:
Vector([[1, 2, 3],
        [4, 5, 6]])

Fill nan elements of a Vector

InĀ [24]:
import py3d
py3d.Vector([0, 1, float("nan")]).fillna(-1)
Out[24]:
Vector([ 0.,  1., -1.])

Drop duplicates

InĀ [25]:
import py3d
py3d.Vector([
    [1,2,1],
    [2,3,4],
    [1,2,1],
    [5,3,4]
]).unique()
Out[25]:
Vector([[1, 2, 1],
        [2, 3, 4],
        [5, 3, 4]])

Read an image

InĀ [26]:
import py3d
py3d.read_img("./20220917214012.jpg").as_image()
Out[26]:

Read a txt file storing poses

InĀ [27]:
import py3d
poses = py3d.read_txt("poses.txt")
poses
Out[27]:
Vector([[   1.        ,   -0.38009858,   -0.92467821,   -0.02225479,  846.2467971 ,   -0.92381071,    0.38071388,   -0.04038213, 3725.075626  ,    0.04581318,    0.00521002,   -0.99893644,  115.2776042 ],
        [   9.        ,   -0.33478138,   -0.94192187,   -0.02654461,  846.339549  ,   -0.94114833,    0.33563081,   -0.03989722, 3725.29806   ,    0.04648925,    0.01162557,   -0.99885114,  115.2612811 ],
        [  14.        ,   -0.29296799,   -0.95575796,   -0.02639101,  846.4037923 ,   -0.95501915,    0.293844  ,   -0.03992654, 3725.508866  ,    0.04591495,    0.01350672,   -0.99885404,  115.2465809 ],
        [  22.        ,   -0.25582311,   -0.96643387,   -0.02366668,  846.4521737 ,   -0.96565419,    0.25661472,   -0.04075369, 3725.723345  ,    0.04545896,    0.0124281 ,   -0.9988889 ,  115.2352558 ],
        [  30.        ,   -0.22198089,   -0.97488512,   -0.01798596,  846.4980023 ,   -0.97409612,    0.22254193,   -0.04014767, 3725.955861  ,    0.043142  ,    0.00860804,   -0.99903187,  115.2302131 ],
        [  35.        ,   -0.18959113,   -0.98172114,   -0.01669745,  846.5432516 ,   -0.98076473,    0.19015581,   -0.04406021, 3726.191177  ,    0.04642996,    0.00802284,   -0.99888933,  115.2168146 ],
        [  53.        ,   -0.15254459,   -0.98823256,   -0.01124982,  846.5785177 ,   -0.98779343,    0.15282023,   -0.03016809, 3726.412478  ,    0.03153229,    0.00651051,   -0.99948153,  115.2403334 ],
        [  57.        ,   -0.1082222 ,   -0.99412667,   -0.00034593,  846.5941848 ,   -0.99364056,    0.10818015,   -0.03123283, 3726.643638  ,    0.03108681,   -0.00303635,   -0.99951208,  115.2390776 ],
        [  61.        ,   -0.05474059,   -0.99850018,   -0.00092533,  846.621932  ,   -0.99832163,    0.0547483 ,   -0.01888239, 3726.877597  ,    0.01890473,   -0.00010986,   -0.99982128,  115.2613816 ],
        [  65.        ,    0.00232874,   -0.9999955 ,    0.00189192,  846.6288611 ,   -0.99995916,   -0.00234517,   -0.00872806, 3727.13067   ,    0.00873246,   -0.00187151,   -0.99996012,  115.2742539 ]])
InĀ [28]:
import py3d
poses[:, [4,8,12]].to_txt("traj.txt", delimiter=" ")

Write csv file

InĀ [29]:
import py3d
py3d.Vector([[1,2,3],[4,5,6]], columns=["t","x","y"]).to_csv("with_cols.csv")
py3d.Vector([[7,8.4,9],[-1,-2,-3]]).to_csv("no_cols.csv")

Read csv file

InĀ [30]:
import py3d
a=py3d.read_csv("with_cols.csv", header=1)
a["t"], a["t","x"], a[1,2]
Out[30]:
(Vector([1., 4.]),
 Vector([[1., 2.],
         [4., 5.]]),
 6.0)
InĀ [31]:
import py3d
py3d.read_csv("no_cols.csv")
Out[31]:
Vector([[ 7. ,  8.4,  9. ],
        [-1. , -2. , -3. ]])

Read pcd file

InĀ [32]:
import py3d
py3d.read_pcd("ascii.pcd").xyz.as_point(py3d.Color(r=1,g=0.5,a=0.3))
Out[32]:
InĀ [33]:
import py3d
pcd = py3d.read_pcd("binary.pcd")
print("min", pcd.min())
print("max", pcd.max())
pcd.xyz.as_point(colormap=pcd.w)
min -109.0828628540039
max 1223933577.0853775
Out[33]:
InĀ [34]:
pcd.sample(10)
Out[34]:
Vector([[-1.93263340e+01,  4.98625898e+00,  4.77467448e-01,  1.28000000e+02,  1.22393358e+09],
        [-1.12955112e+01, -3.58883643e+00, -1.71539736e+00,  1.38000000e+02,  1.22393358e+09],
        [ 4.91373873e+00, -6.98817873e+00, -6.40559852e-01,  1.15000000e+02,  1.22393358e+09],
        [-3.78320426e-01, -6.14186811e+00, -7.11044371e-02,  1.40000000e+02,  1.22393358e+09],
        [-7.42660403e-01,  6.60466528e+00, -2.53517604e+00,  1.49000000e+02,  1.22393358e+09],
        [ 2.27070122e+01,  6.04566336e+00, -2.98854089e+00,  1.26000000e+02,  1.22393358e+09],
        [-1.36021554e+00, -5.82125521e+00, -6.05649710e-01,  1.70000000e+02,  1.22393358e+09],
        [-6.28179550e+00,  6.24954891e+00, -2.40117383e+00,  1.80000000e+02,  1.22393358e+09],
        [ 1.39850569e+01,  1.08020382e+01, -3.53487468e+00,  9.90000000e+01,  1.22393358e+09],
        [-1.62022552e+01,  8.86173820e+00,  2.43441135e-01,  1.59000000e+02,  1.22393358e+09]])

split the Vector into 3 parts

InĀ [35]:
pcd.split([3,4])
Out[35]:
[Vector([[ -9.84686852,  25.20105171,  -2.90541101],
         [-11.15411758,  26.11715317,  -2.88157201],
         [-36.65405273,  72.06262207,   1.20190442],
         ...,
         [-10.16968441,  20.41918373,  -3.34819937],
         [ -5.50718451,  13.51468945,  -3.03626227],
         [ -6.40060711,  13.55139732,  -2.96159911]]),
 Vector([[164.],
         [176.],
         [215.],
         ...,
         [153.],
         [141.],
         [ 81.]]),
 Vector([[1.22393358e+09],
         [1.22393358e+09],
         [1.22393358e+09],
         ...,
         [1.22393358e+09],
         [1.22393358e+09],
         [1.22393358e+09]])]

Write npy file

InĀ [36]:
import py3d
py3d.Vector([1,2,3]).to_npy("tmp.npy")

Read npy file

InĀ [37]:
import py3d
py3d.read_npy("tmp.npy")
Out[37]:
Vector([1, 2, 3])

Read a real lidar point cloud from a npy file

InĀ [38]:
import py3d
py3d.read_npy("lidar.npy").xyz.as_point()
Out[38]:

Read a depth map from a npy file

InĀ [39]:
import py3d
py3d.read_npy("depth.npy").z.as_image()
Out[39]:

Calculate chamfer distance between two points

InĀ [40]:
import py3d
a = py3d.Vector([[0.38902577, 0.97576158, 0.50699271, 1],
                 [0.63387973, 0.58355272, 0.12138496, 0]])
b = py3d.Vector([[0.43179828, 1.0648139, 0.58688312, 1],
                 [0.69850221, 0.67471377, 0.18104768, 1],
                 [0.99474279, 0.20892464, 0.35862561, 2],
                 [0.52789329, 0.81794455, 0.59196469, 0.5],
                 [0.78926438, 0.95307471, 0.31530688, 1]])
py3d.chamfer_distance(a, b, 0.15, True)
Out[40]:
(0.8337985950067301,
 0.28571428571428575,
 array([0.12705228, 0.73322434]),
 array([0.12705228, 0.54096729, 1.40603869, 0.54900878, 0.44435245]))